import 'package:aio/aio.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:app/generated/app_strings.dart';
import 'package:app/src/http/bean/wroks/delivery_notice.dart';
import 'package:app/src/http/bean/wroks/order_item_info.dart';
import 'package:app/src/http/bean/wroks/product_bean.dart';
import 'package:app/src/http/bean/wroks/special_clauses.dart';
import 'package:app/src/ui/route/works/public/items/ui_base.dart';
import 'package:app/src/ui/route/works/public/items/ui_card.dart';
import 'package:app/src/utils/a_combined_utils.dart';
import 'package:app/src/utils/image_path_utils.dart';

import '../../../../app_ui.dart';

///发货审核：发货单明细
class UiDeliveryNoticeItemInfo {
  static Widget build(BuildContext context, DeliveryList1? data,
      {String? title}) {
    if (StringUtils.isEmpty(data)) {
      return AppUi.getEmptyView();
    }
    List<Widget> children = [];
    if (StringUtils.isEmpty(title)) {
      title = AppStrings.of(context).DeliveryList1; //发货单明细
    }
    children.add(
        UiBase.title(context, title, bottom: AppEdgeInsets.margin_bottom)); //标题
    children.add(_table(context, data!));
    var column = UiBase.column(context, children);
    var card = UiCard.build(context, column);
    return card;
  }

  static Widget _table(
    BuildContext context,
    DeliveryList1 data,
  ) {
    return StatefulBuilder(
      builder: (BuildContext context, StateSetter setState) {
        List<TableRow> children = [];
        //==========================================================================标题
        children.add(TableRow(
            //第一行，添加背景色
            decoration:
                BoxDecoration(color: AppColors.backgroundColorWithTableTitle),
            children: _titles(context)));
        var datas = data?.DeliveryDetails;
        if (datas != null && datas.length > 0) {
          var postion = 1;
          datas.forEach((element) {
            //======================================================================内容
            var cells = _contents(context, postion, element, setState);
            postion++;
            children.add(TableRow(children: cells));
            if (element.isOpenAn) {
              children.add(TableRow(
                  //二级标题
                  decoration: BoxDecoration(
                      color: AppColors.backgroundColorWithTabelSecondary),
                  children: _titles2(context)));
              //张开，显示二级详情
              var postion2 = 1;
              element.getOrderDetails2()?.forEach((element) {
                var secondaryContents =
                    _secondaryContents(context, postion2, element);
                children.add(TableRow(
                    children: secondaryContents,
                    decoration: BoxDecoration(
                        color: AppColors.getColorWithTableRow(postion2))));
                postion2++;
              });

              ///小计
              if (element != null) {
                children.add(TableRow(
                    //最后一行，添加背景色
                    decoration: BoxDecoration(
                        color: AppColors.backgroundColorWithTableAcom),
                    children: _aCombined2(context, element!)));
              }
            }
          });
        }

        ///合计
        if (data != null) {
          children.add(TableRow(
              //最后一行，添加背景色
              decoration:
                  BoxDecoration(color: AppColors.backgroundColorWithTableAcom),
              children: _aCombined(context, data!)));
        }
        var table = Table(
          defaultVerticalAlignment: TableCellVerticalAlignment.middle,
          columnWidths: {
            0: FixedColumnWidth(50),
            1: FixedColumnWidth(50),
            2: FixedColumnWidth(350),
            3: FixedColumnWidth(650),
            4: FixedColumnWidth(500),
          },
          textDirection: TextDirection.ltr,
          border: AppStyle.getTableBorder(),
          children: children,
        );
        var width = 2500.0;
        if (width < px.screenWidth()) {
          width = px.screenWidth();
        }
        return SingleChildScrollView(
          scrollDirection: Axis.horizontal,
          physics: BouncingScrollPhysics(),
          child: Container(
            width: width,
            child: table,
          ),
        );
      },
    );
  }

  ///标题
  static List<Widget> _titles(BuildContext context) {
    List<Widget> children = [];
    children.add(_addItem(AppStrings.of(context).Operation)); //操作
    children.add(_addItem(AppStrings.of(context).TheSerial)); //序号
    children.add(_addItem(AppStrings.of(context).OrderCode)); //订单编号
    children.add(_addItem(AppStrings.of(context).FactoryName)); //生产工厂
    children.add(_addItem(AppStrings.of(context).DeliveryProgress)); //发货进度
    children.add(_addItem(AppStrings.of(context).ProductName)); //货品名称
    return children;
  }

  ///内容
  static List<Widget> _contents(BuildContext context, int postion,
      DeliveryDetail2 element, StateSetter setState) {
    List<Widget> children = [];
    if (!element.isOpenAn) {
      children.add(InkWell(
        child: Center(
          child: Icon(
            Icons.arrow_drop_down,
            color: Colors.blueAccent,
          ),
        ),
        onTap: () {
          //展开
          setState(() {
            element.isOpenAn = true;
          });
        },
      )); //操作
    } else {
      children.add(InkWell(
        child: Center(
          child: Icon(
            Icons.arrow_drop_up,
            color: Colors.blueAccent,
          ),
        ),
        onTap: () {
          //关闭
          setState(() {
            element.isOpenAn = false;
          });
        },
      )); //操作
    }
    children.add(_addItem((postion).toString())); //序号
    children.add(_addItem(element.OrderCode)); //订单编号
    children.add(_addItem(element.FactoryName)); //生产工厂
    children.add(_addItem(element.DeliveryProgress)); //发货进度
    children.add(_addItem(element.ProductName)); //货品名称
    return children;
  }

  ///二级标题
  static List<Widget> _titles2(BuildContext context) {
    List<Widget> children = [];
    children.add(_addItem("")); //操作
    children.add(_addItem(AppStrings.of(context).TheSerial)); //序号
    //订单编号
    List<Widget> children1 = [];
    children1.add(_addItem(AppStrings.of(context).CodeName)); //单号
    children1
        .add( _addItem(AppStrings.of(context).Priority)); //优先级
    TableRow row1 = TableRow(
      children: children1,
    );
    var tab1 = Table(
      children: [row1],
      border: AppStyle.getTableBorderInner(),
      columnWidths: {
        0: FlexColumnWidth(1.5),
      },
      defaultVerticalAlignment: TableCellVerticalAlignment.middle,
    );
    children.add(tab1);
    //生产工厂
    List<Widget> children2 = [];
    children2
        .add( _addItem(AppStrings.of(context).Typename)); //类型
    children2
        .add(_addItem(AppStrings.of(context).Itemcode)); //编号
    children2.add(
        _addItem(AppStrings.of(context).Width_Height)); //规格型号
    children2
        .add(_addItem(AppStrings.of(context).CanOutNum)); //数量
    TableRow row2 = TableRow(
      children: children2,
    );
    var tab2 = Table(
      children: [row2],
      border: AppStyle.getTableBorderInner(),
      defaultVerticalAlignment: TableCellVerticalAlignment.middle,
      columnWidths: {
        1: FlexColumnWidth(2),
        2: FlexColumnWidth(1.2),
      },
    );
    children.add(tab2);
    //发货进度
    List<Widget> children3 = [];
    children3.add(_addItem(AppStrings.of(context).Area)); //面积
    children3
        .add(_addItem(AppStrings.of(context).Price2)); //单价
    children3
        .add(_addItem(AppStrings.of(context).Amount)); //金额
    TableRow row3 = TableRow(
      children: children3,
    );
    var tab3 = Table(
      children: [row3],
      border: AppStyle.getTableBorderInner(),
      defaultVerticalAlignment: TableCellVerticalAlignment.middle,
    );
    children.add(tab3);
    //货品名称
    List<Widget> children4 = [];
    children4
        .add(_addItem(AppStrings.of(context).ShelfCode)); //架卡号
    children4
        .add(_addItem(AppStrings.of(context).BoxOutCode)); //箱号
    children4
        .add(_addItem(AppStrings.of(context).Listbz)); //备注
    TableRow row4 = TableRow(
      children: children4,
    );
    var tab4 = Table(
      children: [row4],
      border: AppStyle.getTableBorderInner(),
      defaultVerticalAlignment: TableCellVerticalAlignment.middle,
      columnWidths: {
        2: FlexColumnWidth(2),
      },
    );
    children.add(tab4);
    return children;
  }

  static var _itemHeight = 25.0;

  ///二级内容
  static List<Widget> _secondaryContents(
      BuildContext context, int postion, List<OrderDetail3> element) {
    OrderDetail3 data = element[0];
    List<Widget> children = [];
    children.add(_addItem("")); //操作
    children.add(_addItem((postion).toString())); //序号
    var itemHeight = _itemHeight * element.length; //根据数据个数，计算高度。
    //fixme 订单编号
    List<Widget> children1 = [];
    children1.add(_addItem(data.getCodeName())); //单号
    if (data.isPriority()) {
      //优先发货
      children1.add(Container(
        height: itemHeight,
        child: _addItem(data.Priority, textColor: Colors.white),
        color: Colors.red,
      )); //优先级
    } else {
      //其他可发
      children1.add(Container(
        height: itemHeight,
        child: _addItem(data.Priority, textColor: Colors.white),
        color: Colors.green,
      )); //优先级
    }
    TableRow row1 = TableRow(
      children: children1,
    );
    var tab1 = Table(
      children: [row1],
      border: AppStyle.getTableBorderInner(),
      columnWidths: {
        0: FlexColumnWidth(1.5),
      },
      defaultVerticalAlignment: TableCellVerticalAlignment.middle,
    );
    children.add(tab1);
    //fixme 生产工厂
    List<TableRow> row2s = [];
    element?.forEach((element) {
      List<Widget> children2 = [];
      children2.add(Container(
        child: _addItem(element.Typename),
        height: _itemHeight,
      )); //类型
      children2.add(_addItem(element.Itemcode)); //编号
      children2.add(_addItem(element.Width_Height)); //规格型号
      children2.add(_addItem(element.CanOutNum)); //数量
      TableRow row2 = TableRow(
        children: children2,
      );
      row2s.add(row2);
    });
    var tab2 = Table(
      children: row2s,
      border: AppStyle.getTableBorderInner(),
      defaultVerticalAlignment: TableCellVerticalAlignment.middle,
      columnWidths: {
        1: FlexColumnWidth(2),
        2: FlexColumnWidth(1.2),
      },
    );
    children.add(tab2);
    //fixme 发货进度
    List<TableRow> row3s = [];
    element.forEach((element) {
      List<Widget> children3 = [];
      children3.add(Container(
        child: _addItem(element.Area),
        height: _itemHeight,
      )); //面积
      children3.add(_addItem(element.Price)); //单价
      children3.add( _addItem(element.Amount)); //金额
      TableRow row3 = TableRow(
        children: children3,
      );
      row3s.add(row3);
    });
    var tab3 = Table(
      children: row3s,
      border: AppStyle.getTableBorderInner(),
      defaultVerticalAlignment: TableCellVerticalAlignment.middle,
    );
    children.add(tab3);
    //fixme 货品名称
    List<TableRow> row4s = [];
    element.forEach((element) {
      List<Widget> children4 = [];
      children4.add( Container(
        child: _addItem(element.ShelfCode),
        height: _itemHeight,
      )); //架卡号
      children4.add( _addItem(element.BoxOutCode)); //箱号
      children4.add( _addItem(element.Listbz)); //备注
      TableRow row4 = TableRow(
        children: children4,
      );
      row4s.add(row4);
    });
    var tab4 = Table(
      children: row4s,
      border: AppStyle.getTableBorderInner(),
      defaultVerticalAlignment: TableCellVerticalAlignment.middle,
      columnWidths: {
        2: FlexColumnWidth(2),
      },
    );
    children.add(tab4);
    return children;
  }

  ///小计（二级列表合计）
  static List<Widget> _aCombined2(BuildContext context, DeliveryDetail2 datas) {
    List<Widget> children = [];
    children.add(_addItem(AppStrings.of(context).ACombined2)); //小计
    var DArea = "${AppStrings.of(context).DArea}:${datas?.DArea}"; //面积
    var DCount = "${AppStrings.of(context).DCount}:${datas?.DCount}"; //数量
    var DWeight = "${AppStrings.of(context).DWeight}:${datas?.DWeight}"; //总重量
    var DMoney = "${AppStrings.of(context).DMoney}:${datas?.DMoney}"; //金额
    children.add(_addItem("")); //序号
    //var acom=_text("$TArea\t$TCount\t$TWeight\t$TMoney");
    children.add(_addItem("$DArea")); //订单编号,面积
    children.add(_addItem("$DCount")); //生产工厂,数量
    children.add(_addItem("$DWeight")); //发货进度,重量
    children.add(_addItem("$DMoney")); //货品名称,金额
    return children;
  }

  ///总合计
  static List<Widget> _aCombined(BuildContext context, DeliveryList1 datas) {
    List<Widget> children = [];
    children.add(_addItem(AppStrings.of(context).ACombined)); //合计
    var TArea = "${AppStrings.of(context).TArea}:${datas?.TArea}"; //总面积
    var TCount = "${AppStrings.of(context).TCount}:${datas?.TCount}"; //总数量
    var TWeight = "${AppStrings.of(context).TWeight}:${datas?.TWeight}"; //总重量
    var TMoney = "${AppStrings.of(context).TMoney}:${datas?.TMoney}"; //总金额
    children.add(_addItem("")); //序号
    //var acom=_text("$TArea\t$TCount\t$TWeight\t$TMoney");
    children.add(_addItem("$TArea")); //订单编号,总面积
    children.add(_addItem("$TCount")); //生产工厂,总数量
    children.add(_addItem("$TWeight")); //发货进度,总重量
    children.add(_addItem("$TMoney")); //货品名称,总金额
    return children;
  }

  static Widget _addItem(String? text,
      {bool isBlue = false,
      Color? textColor = null,
      GestureTapCallback? onTap}) {
    Widget child =
        Center(child: _text(text, isBlue: isBlue, textColor: textColor));
    if (onTap == null) {
      return child;
    }
    return InkWell(
      child: child,
      onTap: onTap,
    );
  }

  static Widget _text(String? text,
      {bool isBlue = false, Color? textColor = null}) {
    TextStyle? style = null;
    if (isBlue) {
      style = AppStyle.textStyleWithCardBlue();
    } else {
      style = AppStyle.textStyleWithCard();
    }
    if (textColor != null) {
      style = AppStyle.textStyleWithCard(color: textColor);
    }
    return Text(
      text?.trim() ?? "",
      style: style,
      textAlign: TextAlign.center,
    );
  }

  ///小图标显示
  static Widget _icon(String? url, GestureTapCallback? onTap) {
    if (StringUtils.isEmpty(url)) {
      return AppUi.getEmptyView();
    }
    return InkWell(
      child: Image.network(
        url!,
        height: 40,
      ),
      onTap: () {
        onTap?.call();
      },
    );
  }
}
